package com.ruoyi.commodity.service.impl;


import com.ruoyi.commodity.domain.Commodity;
import com.ruoyi.commodity.domain.vo.CommodityQueryVo;
import com.ruoyi.commodity.domain.vo.CommodityVo;
import com.ruoyi.commodity.mapper.CommodityMapper;
import com.ruoyi.commodity.mapper.CommodityTypeMapper;
import com.ruoyi.commodity.service.ICommodityService;
import com.ruoyi.common.core.constant.CommodityConstants;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.List;
import java.util.Objects;

@Service
public class CommodityServiceImpl implements ICommodityService {

    @Autowired
    public CommodityMapper commodityMapper;

    @Autowired
    public CommodityTypeMapper commodityTypeMapper;


    @Override
    public boolean checkCommodityNameUnique(Commodity commodity) {
        Long commodityId = StringUtils.isNull(commodity.getCommodityId()) ? -1 :commodity.getCommodityId();
        Commodity commodityCheck =  commodityMapper.checkCommodityNameUnique(commodity.getName());
        if (commodityCheck != null && !Objects.equals(commodityCheck.getCommodityId(), commodityId)) {
            return CommodityConstants.NOT_UNIQUE;
        }
        return CommodityConstants.UNIQUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCommodity(Commodity commodity) {
        commodity.setPrice(commodity.getPrice()*100);
        commodity.setMarketPrice(commodity.getMarketPrice()*100);
        commodity.setCostPrice(commodity.getCostPrice()*100);
        return commodityMapper.insertCommodity(commodity);
    }

    @Override
    public int deleteCommodityById(Long commodityId) {
        return commodityMapper.deleteCommodityById(commodityId);
    }

    @Override
    public CommodityVo selectCommodityById(Long commodityId) {
        return commodityMapper.selectCommodityById(commodityId);
    }

    @Override
    public int updateCommodity(Commodity commodity) {
        return commodityMapper.updateCommodity(commodity);
    }

    @Override
    public List<CommodityVo> selectCommodityList(CommodityQueryVo commodityQueryVo) {
        return commodityMapper.selectCommodityList(commodityQueryVo);
    }



}
